package pixelradar.gui;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.texture.TextureManager;
import pixelradar.util.Reference;
import pixelradar.util.Render;

public abstract class ScrollableField extends Gui {
    public static int arrowsWidth = 7;
    public final FontRenderer fontrendererObj;
    public int x;
    public int y;
    public int width;
    public int labelX;
    public int labelY;
    public int labelWidth;
    public int labelHeight;
    public String label;
    private boolean drawArrows = false;
    private int leftArrowX;
    private int rightArrowX;
    private int arrowsY;
    private int arrowsHeight = 12;

    public ScrollableField(int x, int y, int width, String label, FontRenderer fontrendererObj) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.fontrendererObj = fontrendererObj;
        this.label = label;
        this.leftArrowX = this.x + 1;
        this.rightArrowX = this.x + (this.width - arrowsWidth);
        this.arrowsY = this.y;
        this.labelWidth = fontrendererObj.getStringWidth(this.label);
        this.labelHeight = this.fontrendererObj.FONT_HEIGHT;
        this.labelX = this.x - this.labelWidth;
        this.labelY = this.y + this.labelHeight / 2 - 2;
    }

    public void draw() {
        TextureManager renderEngine = Minecraft.getMinecraft().renderEngine;
        this.drawString(this.fontrendererObj, this.label, this.labelX, this.labelY, 16777215);
        if (this.drawArrows) {
            renderEngine.bindTexture(Reference.leftArrowTexture);
            Render.drawTexturedRect((double) this.leftArrowX, (double) this.arrowsY, (double) arrowsWidth, (double) this.arrowsHeight, 0.0D, 0.0D, 1.0D, 1.0D);
            renderEngine.bindTexture(Reference.rightArrowTexture);
            Render.drawTexturedRect((double) this.rightArrowX, (double) this.arrowsY, (double) arrowsWidth, (double) this.arrowsHeight, 0.0D, 0.0D, 1.0D, 1.0D);
        }

    }

    public void setDrawArrows(boolean value) {
        this.drawArrows = value;
    }

    public void mouseClicked(int x, int y, int button) {
        int direction = this.posWithinArrows(x, y);
        if (direction == 1) {
            this.nextElement();
        } else if (direction == -1) {
            this.previousElement();
        }

    }

    public int posWithinArrows(int x, int y) {
        if (x >= this.leftArrowX && y >= this.arrowsY && x <= arrowsWidth + this.leftArrowX && y <= this.arrowsHeight + this.arrowsY) {
            return -1;
        } else {
            return x >= this.rightArrowX && y >= this.arrowsY && x <= arrowsWidth + this.rightArrowX && y <= this.arrowsHeight + this.arrowsY ? 1 : 0;
        }
    }

    public abstract void nextElement();

    public abstract void previousElement();

    public abstract void setFocused(Boolean var1);

    public abstract Boolean isFocused();
}
